package cn.dakaqi.activity.diary.services;

import cn.dakaqi.activity.services.ActivityService;
import cn.dakaqi.entities.Activity;
import cn.dakaqi.entities.ActivityDiary;
import cn.dakaqi.entities.Group;
import cn.dakaqi.group.services.GroupService;
import cn.dakaqi.log.services.LogService;
import cn.dakaqi.member.service.VolunteerService;
import cn.dakaqi.utils.Clock;
import cn.dakaqi.utils.DKQConstant;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.Map;

/**
 * Created by chunyang on 2016/4/18.
 */
@Component(value = "activityDiaryService")
@Transactional
public class ActivityDiaryServiceImpl implements ActivityDiaryService
{
    @Autowired
    GroupService groupService;
    @Autowired
    ActivityDiaryDao activityDiaryDao;
    @Autowired
    private VolunteerService volunteerService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private LogService logService;

    private void saveLog(String message)
    {
        logService.save(400,message);
    }
    @Override
    public void save(String param) throws ServiceRuntimeException
    {
        JSONObject jsonObject = JSON.parseObject(param);
        String memberCode = jsonObject.getString("memberCode");
        String activityCode = jsonObject.getString("activityCode");

        String message = jsonObject.getString("message");
        String imgs = jsonObject.getString("imgs");
        int type = jsonObject.getIntValue("type");

        ActivityDiary activityDiary = new ActivityDiary();
        activityDiary.setCreateTime(Clock.DEFAULT.getCurrentDate());
        activityDiary.setMessage(message);
        Activity activity = this.activityService.findByActivityCode(activityCode);
        activityDiary.setVolunteer(this.volunteerService.findByMemberCode(memberCode));
        activityDiary.setActivity(activity);
        activityDiary.setGroup(activity.getGroup());
        activityDiary.setType(type);
        activityDiary.setImgs(imgs);

        this.save(activityDiary);
    }

    @Override
    public void save(ActivityDiary activityDiary) throws ServiceRuntimeException
    {
        Activity activity = activityDiary.getActivity();
        Group group = activityDiary.getActivity().getGroup();
        switch (activityDiary.getType())
        {
            case DKQConstant.COMMENT_TYPE_GOOD:
                activity.setGoodComments(activity.getGoodComments()+1);
                group.setGoodComment(group.getGoodComment()+1);
                break;
            case DKQConstant.COMMENT_TYPE_MID:
                activity.setMidComments(activity.getMidComments()+1);
                group.setMediumComment(group.getMediumComment()+1);
                break;
            case DKQConstant.COMMENT_TYPE_BAD:
                group.setBadComment(group.getBadComment()+1);
                activity.setBadComments(activity.getBadComments()+1);
        }

        this.activityService.update(activity);
        this.groupService.update(group);
        this.activityDiaryDao.save(activityDiary);

        saveLog("用户【" + activityDiary.getVolunteer().getId() + "】发布了一个活动日记 【" + activityDiary.getId() + "】");
    }

    @Override
    public void update(ActivityDiary activityDiary) throws ServiceRuntimeException
    {
        this.activityDiaryDao.save(activityDiary);
        saveLog("用户【" + activityDiary.getVolunteer().getId() + "】更新了一个活动日记 【" + activityDiary.getId() + "】");
    }

    @Override
    public Page<ActivityDiary> findByActivityCode(String activityCode,int type, int pageNumber) throws ServiceRuntimeException
    {

        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(pageNumber - 1, 20, sort);

        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("activity.activityCode", new SearchFilter("activity.activityCode", SearchFilter.Operator.EQ, activityCode));
        if(type == DKQConstant.COMMENT_TYPE_GOOD || type == DKQConstant.COMMENT_TYPE_BAD || type == DKQConstant.COMMENT_TYPE_MID)
            filters.put("type", new SearchFilter("type", SearchFilter.Operator.EQ, type));

        Specification<ActivityDiary> spec = DynamicSpecifications.bySearchFilter(filters.values(), ActivityDiary.class);
        return this.activityDiaryDao.findAll(spec, pageRequest);
    }

    @Override
    public Page<ActivityDiary> findByMemberCode(String memberCode,int type, int pageNumber) throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(pageNumber - 1, 20, sort);

        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("volunteer.memberCode", new SearchFilter("volunteer.memberCode", SearchFilter.Operator.EQ, memberCode));
        if(type == DKQConstant.COMMENT_TYPE_GOOD || type == DKQConstant.COMMENT_TYPE_BAD || type == DKQConstant.COMMENT_TYPE_MID)
            filters.put("type", new SearchFilter("type", SearchFilter.Operator.EQ, type));
        Specification<ActivityDiary> spec = DynamicSpecifications.bySearchFilter(filters.values(), ActivityDiary.class);
        return this.activityDiaryDao.findAll(spec, pageRequest);
    }

    @Override
    public Page<ActivityDiary> findByGroupCode(String groupCode,int type, int pageNumber) throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(pageNumber - 1, 20, sort);

        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("group.groupCode", new SearchFilter("group.groupCode", SearchFilter.Operator.EQ, groupCode));
        if(type == DKQConstant.COMMENT_TYPE_GOOD || type == DKQConstant.COMMENT_TYPE_BAD || type == DKQConstant.COMMENT_TYPE_MID)
            filters.put("type", new SearchFilter("type", SearchFilter.Operator.EQ, type));
        Specification<ActivityDiary> spec = DynamicSpecifications.bySearchFilter(filters.values(), ActivityDiary.class);
        return this.activityDiaryDao.findAll(spec, pageRequest);
    }
}

